package com.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.filter.OAuthInfo;
import com.app.model.User;
import com.app.service.MenuService;
import com.app.service.OAuthService;
import com.app.service.UserService;


@Controller
@RequestMapping("/web")
public class LoginController {
	@Resource
	private MenuService menuService;
	@Resource
	private OAuthService oAuthService;
	@Resource
	private UserService userService;

	@RequestMapping("/login")
	public String tologin(HttpServletRequest request, HttpServletResponse response,String loginname,
			String password) throws Exception {
		// request.getSession().setAttribute("user", "haha");
		User user = userService.getByName(loginname);
		if (user == null) {
			request.setAttribute("message", "账号不存在,请联系管理员");
			return login(request, response);
		}
		AuthenticationToken token = new UsernamePasswordToken(loginname,
				password, true);
		try {

			SecurityUtils.getSubject().login(token);
		} catch (Exception e) {
			request.setAttribute("message", "token不存在,请联系管理员");
			return login(request, response);
		}
		if (SecurityUtils.getSubject().isAuthenticated()) {
			OAuthInfo info = new OAuthInfo();
			info=oAuthService.genOAuthInfo();
			SecurityUtils.getSubject().getSession(true).setAttribute("OAUTH_INFO", info);
			return "redirect:index";
		}
		request.setAttribute("message", "认证失败,请联系管理员");
		return login(request, response);
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(HttpServletRequest request, HttpServletResponse response) {
		if (SecurityUtils.getSubject().isAuthenticated()) {
			return "redirect:index";
		}
		return "login";
	}

	@RequestMapping("/getUserInfo")
	@ResponseBody
	public Object getUserInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		OAuthInfo info = (OAuthInfo) SecurityUtils.getSubject().getSession(true).getAttribute("OAUTH_INFO");
		return info;
	}

	@RequestMapping("/loginout")
	public String loginout(HttpServletRequest request) throws Exception {
		//request.getSession().invalidate();
		SecurityUtils.getSubject().logout();
		return "login";
	}

	// 显示admin首页
	@RequestMapping("/index")
	public String index() throws Exception {
		return "index";
	}

	@RequestMapping("/com")
	public String com() throws Exception {
		return "com";
	}

	@RequestMapping("/cate")
	public String cate() throws Exception {
		return "cate";
	}

	@RequestMapping("/api")
	public String api() throws Exception {
		return "api";
	}

	@RequestMapping("/mock")
	public String mock() throws Exception {
		return "mock";
	}

	@RequestMapping("/getMenu")
	@ResponseBody
	public Object getMenu() {
		OAuthInfo info = (OAuthInfo) SecurityUtils.getSubject().getSession(true).getAttribute("OAUTH_INFO");
		//return menuService.findAll();
		return info.getList();

	}

	@RequestMapping("/getInfo")
	@ResponseBody
	public Object getInfo(String url) {
		return menuService.findMenu(url);

	}

}
